
//THIS CODE FILE PRODUCES TABLE 1, TABLE A.1, TABLE A.2, TABLE A.3, TABLE A.4, TABLE A.5, AND TABLE A.6

global main "[YOUR PATH HERE]"
global output "${main}/output"


//switch to rerun the build stacked data do file
local switch_rerun_stacking = 0
local switch_controls = 1

local switch_dd = 0 //this runs the table as a DD in addition to an event study 

********************************************************************************
**CREATE STACKED DATA**
********************************************************************************

if `switch_rerun_stacking'{
	do "${main}/code/build_stacked_data.do"
}

********************************************************************************
**ENROLLMENT DATA**
********************************************************************************
foreach c1 in public fponly all{
	use "${main}/data/outcomes_enrollment.dta", clear
	
	if "`c1'" == "public"{
		keep if control == 1
		local ctrl  "_pubonly"
	}
	else if "`c1'" == "fponly"{
		keep if control == 3
		local ctrl  "_fponly"
	}
	else if "`c1'" == "all"{
		local ctrl = "_all"
	}

	tempfile using
		save `using'
		
	foreach l1 in "ftonly" "ptonly" "all"{
		
		use `using', clear
		
		if "`l1'" == "ftonly"{
			keep if efalevel == 22
			local lvl  "_FT"
		}
		else if "`l1'" == "ptonly"{
			keep if efalevel == 42
			local lvl  "_PT"
		}
		else if "`l1'" == "all"{
			local lvl = "_all"
		}


		//collapse by state
		collapse (sum) eftotlt eftotlm eftotlw , by(file_year deg st_fips)
			drop if st_fips > 56 //this drops all of the US territories
			drop if file_year < 1986 // this drops 1980, 1984 and 1985, for which our data are much spottier
			
			
		rename file_year year_academic
		rename st_fips statefips 
		keep year_academic statefips deg eftotlt eftotlm eftotlw
				
		reshape wide eftotlt eftotlm eftotlw , i(year_academic statefips) j(deg)
			foreach x in t m w{
				gen log_eftotl`x'1 = ln(eftotl`x'1)
				gen log_eftotl`x'3 = ln(eftotl`x'3)
			}
			
			
		if `switch_controls'{
			//generate indicators for if a state has a free CC program in a given year
			gen free_cc = 0
				replace free_cc = 1 if statefips == 5  & year_academic > 2016
				replace free_cc = 1 if statefips == 6  & year_academic > 2018
				replace free_cc = 1 if statefips == 9  & year_academic > 2019
				replace free_cc = 1 if statefips == 10 & year_academic > 2005
				replace free_cc = 1 if statefips == 13 & year_academic > 2013
				replace free_cc = 1 if statefips == 15 & year_academic > 2017
				replace free_cc = 1 if statefips == 18 & year_academic > 1990
				replace free_cc = 1 if statefips == 19 & year_academic > 2019
				replace free_cc = 1 if statefips == 20 & year_academic > 2016
				replace free_cc = 1 if statefips == 21 & year_academic > 2016
				replace free_cc = 1 if statefips == 22 & year_academic > 2016
				replace free_cc = 1 if statefips == 24 & year_academic > 2016
				replace free_cc = 1 if statefips == 28 & year_academic > 2016
				replace free_cc = 1 if statefips == 29 & year_academic > 2016
				replace free_cc = 1 if statefips == 30 & year_academic > 2016
				replace free_cc = 1 if statefips == 32 & year_academic > 2016
				replace free_cc = 1 if statefips == 34 & year_academic > 2016
				replace free_cc = 1 if statefips == 36 & year_academic > 2016
				replace free_cc = 1 if statefips == 40 & year_academic > 2016
				replace free_cc = 1 if statefips == 41 & year_academic > 2016
				replace free_cc = 1 if statefips == 44 & year_academic > 2016
				replace free_cc = 1 if statefips == 46 & year_academic > 2016
				replace free_cc = 1 if statefips == 47 & year_academic > 2016
				replace free_cc = 1 if statefips == 53 & year_academic > 2016
				replace free_cc = 1 if statefips == 54 & year_academic > 2016
			
			//add controls for unemployment rate by state
			preserve
				use "${main}/data/clean_controls_labormarket.dta", clear
			
				keep statefips unemp_rate year
					rename year year_academic
				
				tempfile unemployment
				save `unemployment'
			restore 
		
			merge m:1 year_academic statefips using `unemployment', assert(match using) keep(match) nogen 
			//need to assert match master so when we restrict years we can get rid of those not in our sample
			
			//add controls for log state population (18-30 years old)
			preserve
				use "${main}/data/census_yearly_statepop.dta", clear
			
				tempfile statepop
				save `statepop'
			restore 
		
			merge m:1 year_academic statefips using `statepop', assert(match using) keep(match) nogen 
			//need to assert match master so when we restrict years we can get rid of those not in our sample
						
			keep year_academic statefips eftotl* log_eftotl* free_cc unemp_rate lnpop_1830
			
			tempfile enroll
				save `enroll'
				
			local controls "unemp_rate free_cc lnpop_1830"
			local cont "_ctrla"
		}

		else{
			local ytitle "log enrollment in college"
			local controls ""
			local cont ""
		}
		

		merge 1:m year_academic statefips using "${main}/data/mw_stacked.dta", keep(match) // keep matches drops years outside of our enrollment panel
		
	********************************************************************************
	**BUILDING TABLES**
	********************************************************************************

	matrix col1`ctrl'`lvl' = J(8,2,.)
	matrix col2`ctrl'`lvl' = J(8,2,.)
	matrix col3`ctrl'`lvl' = J(8,2,.)
	matrix col4`ctrl'`lvl' = J(8,2,.)
	matrix col5`ctrl'`lvl' = J(8,2,.)
	matrix col6`ctrl'`lvl' = J(8,2,.)
	matrix col13`ctrl'`lvl' = J(8,1,.)
	matrix results`ctrl'`lvl' = J(8,13,.)
	
	matrix DD`lvl' = J(20,3,.)

	* define treatment vector
	global treat F*treat L*treat

	*linear combinations of post-reg coeffs
	global lincomm3 F3treat - F1treat
	global lincomm2 F2treat - F1treat
	global lincomm1 F1treat - F1treat
	global lincom0 L0treat - F1treat
	global lincom1 L1treat - F1treat
	global lincom2 L2treat - F1treat
	global lincom3 L3treat - F1treat
	global lincom4 L4treat - F1treat
	
	local d = 1
	local cn = 0
	foreach var in log_eftotlt log_eftotlm log_eftotlw{
		local cn = `cn'+1
		eststo twoyr:  reghdfe `var'1 $treat `controls' , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
			local dof = e(df_r)
			local i = 1
					
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col`cn'`ctrl'`lvl'[`i',1] = r(estimate)
						matrix col`cn'`ctrl'`lvl'[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col`cn'`ctrl'`lvl'[`i',1] = r(estimate)
						matrix col`cn'`ctrl'`lvl'[`i++',2] = r(se)
				}
				
				
				if `switch_dd'{
					lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
						matrix DD`lvl'[`d',1] = r(estimate)
						matrix DD`lvl'[`d',2] = r(se)
				}

		local cn = `cn'+1
		eststo fouryr:  reghdfe `var'3 $treat `controls' , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
					local i = 1
					local d = `d'+1
					
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col`cn'`ctrl'`lvl'[`i',1] = r(estimate)
						matrix col`cn'`ctrl'`lvl'[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col`cn'`ctrl'`lvl'[`i',1] = r(estimate)
						matrix col`cn'`ctrl'`lvl'[`i++',2] = r(se)
				}
				
				if `switch_dd'{
					lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
						matrix DD`lvl'[`d',1] = r(estimate)
						matrix DD`lvl'[`d',2] = r(se)
						
					local d = `d'+1
				}
	}
	
		matrix col13`ctrl'`lvl'[1,1] = -3
		matrix col13`ctrl'`lvl'[2,1] = -2
		matrix col13`ctrl'`lvl'[3,1] = -1
		matrix col13`ctrl'`lvl'[4,1] = 0
		matrix col13`ctrl'`lvl'[5,1] = 1
		matrix col13`ctrl'`lvl'[6,1] = 2
		matrix col13`ctrl'`lvl'[7,1] = 3
		matrix col13`ctrl'`lvl'[8,1] = 4

		matrix results`ctrl'`lvl' = col1`ctrl'`lvl', col2`ctrl'`lvl', col3`ctrl'`lvl', col4`ctrl'`lvl', col5`ctrl'`lvl', col6`ctrl'`lvl', col13`ctrl'`lvl'
		
	}
	
	matrix r`ctrl' = J(8,39,.)
	matrix r`ctrl' = results`ctrl'_all, results`ctrl'_FT, results`ctrl'_PT
		
	clear
	matlist r`ctrl'
	svmat r`ctrl'
					
	export excel "${output}/tables_raw.xlsx", sheet(evstd_`ctrl'`cont') sheetreplace firstrow(var)

	
	
	if `switch_dd'{
		matrix r`ctrl'dd = J(60,9,.)
		matrix r`ctrl'dd = DD_all , DD_FT, DD_PT
	
		clear
		matlist r`ctrl'dd
		svmat r`ctrl'dd
					
		export excel "${output}/tables_raw.xlsx", sheet(evstd_`ctrl'`cont'DD) sheetreplace firstrow(var)
	}
}

